﻿using System;
using System.Collections.Generic;
using System.Text;

namespace daSyntaxAnalyser.SyntaxTree
{
    using daBase.Objects;

    public class FunctionTypeElement : ProcedureTypeElement
    {
        private ITypeElement resulttype;

        public ITypeElement ResultType { get { return resulttype; } set { resulttype = value; } }

        public FunctionTypeElement(TokenInfo Type, List<FunctionVarDeclarationElement> Vars, ITypeElement ResultType, bool OfObject, List<DirectiveElement> Directives, List<TokenInfo> Comments)
            : base(Type, Vars, OfObject, Directives, Comments)
        {
            resulttype = ResultType;
        }

        public override string ToString()
        {
            string result = "function ";
            if ((vars != null) && (vars.Count > 0))
                result += string.Format("({0})", vars.Join("; "));
            if (resulttype != null) result += string.Format(" : {0}", resulttype);
            if (ofobject)
                result += " of object";
            if ((directives != null) && (directives.Count > 0))
                result += string.Format("; {0}", directives.Join("; "));
            return result;
        }

        protected override IEnumerator<ISyntaxTreeElement> CreateEnumerator()
        {
            if (vars != null)
                foreach (FunctionVarDeclarationElement var in vars)
                    yield return var;
            if (resulttype != null)
                yield return resulttype;
            if (directives != null)
                foreach (DirectiveElement directive in directives)
                    yield return directive;
        }
    }
}
